// 创建地图实例
const map = new AMap.Map('mapContainer', {
  zoom: 10,
  center: [102.765, 30.074],
  viewMode: '3D', // 开启 3D 视图模式
  features: ['bg', 'road', 'building'] //只展示背景、道路及建筑元素

});

// 创建卫星图层
const satelliteLayer = new AMap.TileLayer.Satellite();
satelliteLayer.setMap(map); // 将卫星图层设置为地图的覆盖物

//监测站信息
const monPoints = [
  { name: "DZ02", type: '监测站', longitude: 102.163463906, latitude: 29.480641693, altitude: 1378.196, Hardpoint: 'DZS03', port: 10291, ip: '223.85.251.32' },
  { name: "DZ03", type: '监测站', longitude: 102.166469024, latitude: 29.478646440, altitude: 1198.829, Hardpoint: 'DZS02', port: 10291, ip: '223.85.251.32' },
  { name: "DZ06", type: '监测站', longitude: 102.703469937, latitude: 30.491647553, altitude: 1358.285, Hardpoint: 'DZS17', port: 10291, ip: '223.85.251.32' },
  { name: "DZ07", type: '监测站', longitude: 102.705467918, latitude: 30.483645083, altitude: 1208.033, Hardpoint: 'DZS18', port: 10291, ip: '223.85.251.32' },
  { name: "DZ08", type: '监测站', longitude: 102.697468299, latitude: 30.488643587, altitude: 1438.231, Hardpoint: 'DZS36', port: 10291, ip: '223.85.251.32' },
  { name: "DZ09", type: '监测站', longitude: 102.740226543, latitude: 29.296545767, altitude: 828.130, Hardpoint: 'DZS32', port: 10291, ip: '223.85.251.32' },
  { name: "DZ11", type: '监测站', longitude: 102.705465449, latitude: 30.495642328, altitude: 1278.503, Hardpoint: 'DZS15', port: 10291, ip: '223.85.251.32' },
  { name: "DZ12", type: '监测站', longitude: 102.721469410, latitude: 30.447643845, altitude: 1318.214, Hardpoint: 'DZS13', port: 10291, ip: '223.85.251.32' },
  { name: "DZ13", type: '监测站', longitude: 102.724467512, latitude: 30.450647447, altitude: 1188.115, Hardpoint: 'DZS12', port: 10291, ip: '223.85.251.32' },
  { name: "DZ15", type: '监测站', longitude: 102.715469346, latitude: 30.448645380, altitude: 1568.136, Hardpoint: 'DZS07', port: 10291, ip: '223.85.251.32' },
  { name: "DZ16", type: '监测站', longitude: 103.014462119, latitude: 30.071643149, altitude: 758.759, Hardpoint: 'DZS22', port: 10291, ip: '223.85.251.32' },
  { name: "DZ17", type: '监测站', longitude: 102.163464077, latitude: 29.479644115, altitude: 1338.240, Hardpoint: 'DZS23', port: 10291, ip: '223.85.251.32' },
  { name: "DZ18", type: '监测站', longitude: 102.161462472, latitude: 29.481647628, altitude: 1408.607, Hardpoint: 'DZS08', port: 10291, ip: '223.85.251.32' },
  { name: "DZ19", type: '监测站', longitude: 103.013466175, latitude: 30.075645866, altitude: 788.085, Hardpoint: 'DZS06', port: 10291, ip: '223.85.251.32' },
  { name: "DZ20", type: '监测站', longitude: 102.543464741, latitude: 29.606642839, altitude: 1938.978, Hardpoint: 'DZS11', port: 10291, ip: '223.85.251.32' },
  { name: "DZ22", type: '监测站', longitude: 102.702461445, latitude: 30.487648352, altitude: 1398.876, Hardpoint: 'DZS31', port: 10291, ip: '223.85.251.32' },
  { name: "DZ23", type: '监测站', longitude: 102.706461481, latitude: 30.487649276, altitude: 1248.421, Hardpoint: 'DZS16', port: 10291, ip: '223.85.251.32' },
  { name: "DZ25", type: '监测站', longitude: 102.540467314, latitude: 29.605645976, altitude: 1638.681, Hardpoint: 'DZS19', port: 10291, ip: '223.85.251.32' },
  { name: "DZ26", type: '监测站', longitude: 103.018468645, latitude: 30.069643133, altitude: 888.860, Hardpoint: 'DZS26', port: 10291, ip: '223.85.251.32' },
  { name: "DZ31", type: '监测站', longitude: 103.014462999, latitude: 30.069647224, altitude: 798.593, Hardpoint: 'DZS43', port: 10291, ip: '223.85.251.32' },
  { name: "DZ32", type: '监测站', longitude: 102.723467341, latitude: 30.452647048, altitude: 1158.063, Hardpoint: 'DZS09', port: 10291, ip: '223.85.251.32' },
  { name: "DZ36", type: '监测站', longitude: 102.720462793, latitude: 30.449649384, altitude: 1328.593, Hardpoint: 'DZS38', port: 10291, ip: '223.85.251.32' },
  { name: "DZ38", type: '监测站', longitude: 102.740480613, latitude: 29.294247784, altitude: 889.404, Hardpoint: 'DZS39', port: 10291, ip: '223.85.251.32' },
  { name: "DZ39", type: '监测站', longitude: 102.740599271, latitude: 29.294785552, altitude: 916.775, Hardpoint: 'DZS40', port: 10291, ip: '223.85.251.32' },
  { name: "DZ41", type: '监测站', longitude: 102.551464254, latitude: 29.598649290, altitude: 1628.560, Hardpoint: 'DZS20', port: 10291, ip: '223.85.251.32' },
  { name: "DZ43", type: '监测站', longitude: 102.740964903, latitude: 29.296836735, altitude: 814.916, Hardpoint: 'DZS21', port: 10291, ip: '223.85.251.32' }
];
//基准站信息
const refPoints = [
  { name: "DZ14", type: '基准站', longitude: 102.669896649, latitude: 29.611001761, altitude: 1768.967, Hardpoint: 'DZS34', port: 10291, ip: '223.85.251.32' },
  { name: "DZ33", type: '基准站', longitude: 102.866586351, latitude: 29.285032600, altitude: 898.933, Hardpoint: 'DZS28', port: 10291, ip: '223.85.251.32' },
  { name: "DZ30", type: '基准站', longitude: 103.161456616, latitude: 30.160031333, altitude: 668.918, Hardpoint: 'DZS27', port: 10291, ip: '223.85.251.32' },
  { name: "DZ27", type: '基准站', longitude: 102.860656825, latitude: 30.480015436, altitude: 1298.919, Hardpoint: 'DZS30', port: 10291, ip: '223.85.251.32' },
  { name: "DZ28", type: '基准站', longitude: 102.862326957, latitude: 30.455028831, altitude: 1368.921, Hardpoint: 'DZS33', port: 10291, ip: '223.85.251.32' }
];
// cors站点数据
const corsPoints = [
  { name: "BAOX", type: 'cors站', longitude: 102.823369242, latitude: 30.247992642, altitude: 752.328, antenna: 'TRM57971.00', port: 10291, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "DANL", type: 'cors站', longitude: 103.488999592, latitude: 30.066817694, altitude: 552.1669, antenna: 'TRM57971.00', port: 10169, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "HANY", type: 'cors站', longitude: 102.570051000, latitude: 29.360491874, altitude: 1565.154, antenna: 'TRM57971.00', port: 10111, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "HOYA", type: 'cors站', longitude: 103.374907320, latitude: 29.893299602, altitude: 554.8159, antenna: 'HXCCGX601A', port: 11066, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "LUDI", type: 'cors站', longitude: 102.246755780, latitude: 29.904741015, altitude: 1584.676, antenna: 'HITAT45101CP', port: 11070, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "MINS", type: 'cors站', longitude: 103.083715714, latitude: 30.051011728, altitude: 654.5296, antenna: 'TRM57971.00', port: 10129, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "NBSH", type: 'cors站', longitude: 103.267803342, latitude: 30.408213595, altitude: 1555.471, antenna: 'HITAT45101CP', port: 11013, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "PUJI", type: 'cors站', longitude: 103.488768925, latitude: 30.201215669, altitude: 558.2615, antenna: 'HITAT45101CP', port: 11075, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "QAQI", type: 'cors站', longitude: 102.764491700, latitude: 30.755471431, altitude: 2508.83, antenna: 'HXCCGX601A', port: 11055, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "QLAI", type: 'cors站', longitude: 103.449252172, latitude: 30.431175539, altitude: 454.5615, antenna: 'TRM59900.00', port: 10331, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "TIAQ", type: 'cors站', longitude: 102.763995389, latitude: 30.074583769, altitude: 753.765, antenna: 'TRM59900.00', port: 10608, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "WAWS", type: 'cors站', longitude: 103.038700586, latitude: 29.576714123, altitude: 1579.425, antenna: 'HXCCGX601A', port: 11049, ip: '127.16.34.9', Hardpoint: 9401 },
  { name: "YINJ", type: 'cors站', longitude: 102.760572356, latitude: 29.764649397, altitude: 753.8143, antenna: 'TRM57971.00', port: 10324, ip: '127.16.34.9', Hardpoint: 9401 }
];
// 添加标注点
function addMarkers(points, iconUrl) {
  points.forEach(point => {
    const marker = new AMap.Marker({
      position: [point.longitude, point.latitude],
      offset: new AMap.Pixel(-13, -30),
      icon: iconUrl,
    });
    marker.setExtData({ data: point });
    marker.on('click', function (e) {
      const point = e.target.getExtData().data;

      // 检查并设置元素值
      function setElementValue(id, value) {
        const element = document.getElementById(id);
        if (element) {
          element.value = value || '';
        }
      }

      // 设置其他字段
      setElementValue('stationName', point.name);
      setElementValue('Hardpoint', point.Hardpoint);
      setElementValue('port', point.port);
      setElementValue('ip', point.ip);
      setElementValue('antenna', point.antenna);

      // 生成随机总时间
      if (!point.totalTime) {
        const h = getRandomInt(0, 23);
        const m = getRandomInt(0, 59);
        const s = getRandomInt(0, 59);
        point.totalTime = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
      }

      // 显示信息窗口
      const infoWindow = new AMap.InfoWindow({
        content: `<div><strong>点名：${point.name}</strong><br>
                  类型：${point.type}<br>
                  经度：${point.longitude.toFixed(3)}°<br>
                  纬度：${point.latitude.toFixed(3)}°<br>
                  海拔：${point.altitude.toFixed(3)}m<br></div>`
      });

      infoWindow.open(map, marker.getPosition());

      // 更新总时间
      let totalTime = point.totalTime;
      const updateTime = () => {
        let [h, m, s] = totalTime.split(':').map(Number);
        s += 1; // 每秒增加
        if (s >= 60) { m++; s -= 60; }
        if (m >= 60) { h++; m -= 60; }
        if (h >= 24) { h -= 24; } // 24小时制
        totalTime = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;

        // 更新HTML元素中的总时间
        setElementValue('totalTime', totalTime);
      };

      // 每秒更新一次时间
      const intervalId = setInterval(updateTime, 1000);

      // 当信息窗口关闭时清除计时器
      infoWindow.on('close', () => {
        clearInterval(intervalId);
      });
    });
    marker.setMap(map);
  });
}

// 绘制Delaunay三角网
function drawDelaunay(points) {
  const coordinates = points.map(p => [p.longitude, p.latitude]);
  const delaunay = d3.Delaunay.from(coordinates);
  const triangles = delaunay.triangles;
  for (let i = 0; i < triangles.length; i += 3) {
    const p1 = points[triangles[i]];
    const p2 = points[triangles[i + 1]];
    const p3 = points[triangles[i + 2]];
    const path = new AMap.Polyline({
      path: [
        [p1.longitude, p1.latitude],
        [p2.longitude, p2.latitude],
        [p3.longitude, p3.latitude],
        [p1.longitude, p1.latitude] // 闭合路径
      ],
      strokeColor: "#50a7da",
      strokeOpacity: 0.8,
      strokeWeight: 2
    });

    path.setMap(map);
  }
}

// window.onload = function () {
// 添加已知点和控制点标注
addMarkers(monPoints, 'images/spot-1.png'); // 更改为监测站图标路径
addMarkers(refPoints, 'images/spot-2.png'); // 更改为基准站图标路径
addMarkers(corsPoints, 'images/spot-3.png'); // 更改为CORS站图标路径

// 调用函数创建三角网
drawDelaunay(corsPoints)
// };

// 生成随机整数
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}








